from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import GoodsChannel, GoodsCategory, GoodsChannelGroup
from apps.meiduo_admin.serializers.channels import GoodsChannelSerializer, GoodsCategorySerializer, \
    GoodsChannelGroupSerializer
from apps.meiduo_admin.utils import UserPageNum


class GoodsChannelView(ModelViewSet):
    '''
        规格选项表的增删改查
    '''
    # 指定查询集
    queryset = GoodsChannel.objects.all()
    # 指定序列化器
    serializer_class = GoodsChannelSerializer
    # 指定分页器
    pagination_class = UserPageNum
    # 指定权限
    permission_classes = [IsAdminUser]

    # 获取一级分类信息
    def categories(self, request):
        data = GoodsCategory.objects.filter(parent=None)
        s = GoodsCategorySerializer(data, many=True)
        return Response(s.data)

    # 获取频道组信息
    def channel_types(self, request):
        data = GoodsChannelGroup.objects.all()
        s = GoodsChannelGroupSerializer(data, many=True)
        return Response(s.data)
